<?php

//重建模块首先加载项目
function ydblog_rebuild_Main() {
	global $zbp;
	$zbp->RegBuildModule('comments','ydblog_side_comm');
	$zbp->RegBuildModule('previous','ydblog_side_previous');
	$zbp->RegBuildModule('catalog','ydblog_catalogsss');
	//$zbp->RegBuildModule('searchpanel','ydblog_side_searchpanel');
	$zbp->RegBuildModule('tags','ydblog_side_Tags');
}

function ydblog_side_previous() {
	global $zbp;
	
	$i = $zbp->modulesbyfilename['previous']->MaxLi;
	if ($i == 0) $i = 10;
	$articles = $zbp->GetArticleList('*', array(array('=', 'log_Type', 0), array('=', 'log_Status', 0)), array('log_PostTime' => 'DESC'), $i, null,false);
	$s = '';
	foreach ($articles as $article) {
	
		if ($zbp->CheckPlugin('IMAGE')){ IMAGE::getPics($article,80,65,4); }
		if ($zbp->CheckPlugin('sf_img1')){ SF_img1::getPics($article,80,65,4); }

		if (($zbp->CheckPlugin('IMAGE')) && ($article->IMAGE_COUNT>0)){
			$s .= '<li><a href="' . $article->Url. '" title="' . $article->Title. '"><img src="' .$article->IMAGE[0]. '" alt="' . $article->Title. '" class="thumbnail" /><div class="hotpost-left"><span class="hot-post-title" >' . $article->Title. '</span><span class="hot-post-info" ><i class="icon-clock"></i>' . $article->Time('Y-m-d'). '</span><span class="hot-post-info" ><i class="icon-eye"></i>' . $article->ViewNums. '</span></div></a></li>';
		}elseif (($zbp->CheckPlugin('sf_img1')) && ($article->sf_img_count>0)){
			$s .= '<li><a href="' . $article->Url. '" title="' . $article->Title. '"><img src="' .$article->sf_img[0]. '" alt="' . $article->Title. '" class="thumbnail" /><div class="hotpost-left"><span class="hot-post-title" >' . $article->Title. '</span><span class="hot-post-info" ><i class="icon-clock"></i>' . $article->Time('Y-m-d'). '</span><span class="hot-post-info" ><i class="icon-eye"></i>' . $article->ViewNums. '</span></div></a></li>';
		}
		else{
			$s .= '<li><a href="' . $article->Url. '" title="' . $article->Title. '"><img src="' .(ydblog_thumbnail($article)). '" alt="' . $article->Title. '" class="thumbnail" /><div class="hotpost-left"><span class="hot-post-title" >' . $article->Title. '</span><span class="hot-post-info" ><i class="icon-clock"></i>' . $article->Time('Y-m-d'). '</span><span class="hot-post-info" ><i class="icon-eye"></i>' . $article->ViewNums. '</span></div></a></li>';	
		}
	}

	return $s;
}
function ydblog_catalogsss() {
	global $zbp;
	$s = '';
	if ($zbp->option['ZC_MODULE_CATALOG_STYLE'] == '2') {
		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 0) {
				$s .= '<li id="navbar-category-'.$value->ID.'" class="submenu li-cate-'.$value->ID.'"><a href="' . $value->Url . '"><i class="icon-right-dir"></i> ' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li>';
			}
		}
		foreach ($zbp->categorysbyorder as $key => $value) {
			if ($value->Level == 1) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li id="navbar-category-'.$value->ID.'" class="li-subcate-'.$value->ID.'"><a href="' . $value->Url . '"> ' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}
		foreach ($zbp->categorysbyorder as $key => $value){
			if($value->Level == 2) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li id="navbar-category-'.$value->ID.'" class="li-subcate-'.$value->ID.'"><a href="' . $value->Url . '"> ' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}foreach($zbp->categorysbyorder as $key => $value){
			if ($value->Level == 3) {
				$s = str_replace('<!--' . $value->ParentID . 'end-->', '<li id="navbar-category-'.$value->ID.'" class="li-subcate-'.$value->ID.'"><a href="' . $value->Url . '"> ' . $value->Name . '</a><!--' . $value->ID . 'begin--><!--' . $value->ID . 'end--></li><!--' . $value->ParentID . 'end-->', $s);
			}
		}foreach($zbp->categorysbyorder as $key => $value){
			$s = str_replace('<!--' . $value->ID . 'begin--><!--' . $value->ID . 'end-->', '', $s);
		}foreach($zbp->categorysbyorder as $key => $value){
			$s = str_replace('<!--' . $value->ID . 'begin-->', '<ul class="sub-menu">', $s);
			$s = str_replace('<!--' . $value->ID . 'end-->', '</ul>', $s);
		}
	}elseif($zbp->option['ZC_MODULE_CATALOG_STYLE'] == '1'){
		foreach ($zbp->categorysbyorder as $key => $value){
			$s .= '<li id="navbar-category-'.$value->ID.'">' . $value->Symbol . '<a href="' . $value->Url . '">' . $value->Name . '</a></li>';
		}
	}else{
		foreach ($zbp->categorysbyorder as $key => $value){
			$s .= '<li id="navbar-category-'.$value->ID.'"><a href="' . $value->Url . '">' . $value->Name . '</a></li>';
		}
	}return $s;
}

//侧栏评论
function ydblog_side_comm() {
    global $zbp;
	
	$i = $zbp->modulesbyfilename['comments']->MaxLi;
	if ($i == 0) $i = 10;
	$comments = $zbp->GetCommentList('*', array(array('=', 'comm_RootID', 0)), array('comm_PostTime' => 'DESC'), $i, null);

	$s = '';
	foreach ($comments as $comment) {
		
		$s .= '<li><a href="' . $comment->Post->Url .'#cmt' . $comment->ID .'" title="查看《'.$comment->Post->Title.'》上的评论" target="_blank">'.$comment->Author->StaticName.'&nbsp;&nbsp;<span class="text-muted">评论说：<br>' . TransferHTML($comment->Content, '[noenter]') . '</span></a></li>';
	}

	return $s;
}

//侧栏按文章数排序TAGS
function ydblog_side_Tags(){
	global $zbp,$str;
	$str = '';
	$i = $zbp->modulesbyfilename['tags']->MaxLi;
	if ($i == 0) $i = 24;
	$array = $zbp->GetTagList('','',array('tag_Count'=>'DESC'),array($i),'');

	foreach ($array as $tag) {
		$str .= '<li><a href="' . $tag->Url . '">' . $tag->Name . '<span class="tag-count"> (' . $tag->Count . ')</span></a></li>';
	}
	return $str;
}

//文章归档调用
function ydblog_page_archive_list(){
	global $zbp;
	$str=file_get_contents($zbp->usersdir . 'cache/ydblog_archive.txt');
	return $str;
}
//读者墙
function ydblog_page_readers(){
	global $zbp;
	$str=file_get_contents($zbp->usersdir . 'cache/ydblog_readers.txt');
	return $str;
}
//标签调用
function ydblog_page_tagss(){
	global $zbp;
	$str=file_get_contents($zbp->usersdir . 'cache/ydblog_tagss.txt');
	return $str;
}
//读者墙页面
function ydblog_page_readers_cache(){
	global $zbp;
	$b = mktime(0,0,0,date('m')-1,date('d'),date('Y'));
	$e = mktime(0,0,0,date('m'),date('d')+1,date('Y'));
	$mail =$zbp->Config('ydblog')->mail;
	$array = $zbp->db->sql->Select(
		$zbp->table['Comment'],
		array('COUNT(comm_ID) AS cnt','comm_Name', 'comm_HomePage','comm_Email'),
		array(
			array('<>', 'comm_Email', $mail),
			array('<>', 'comm_Name', "访客"),
			array('<>', 'comm_Name', "admin"),
			array('BETWEEN', 'comm_PostTime', $b, $e),
			array('CUSTOM','1=1 GROUP BY comm_HomePage')
		),		
		array('cnt' => 'DESC'),50,null);
	$array = $zbp->db->Query($array);
	$s = '';
	foreach ($array as $key=>$comment) {
		$i=$key+1;
		$avatarload = '<img data-src="http://www.gravatar.com/avatar/' .md5(strtolower($comment['comm_Email'])).'" class="avatar avatar-50" height="50" width="50" />';
		$avatarurl = 'href="'.$comment['comm_HomePage'] . '" rel="external nofollow" title="' . $comment['comm_Name'] . '(评论' . $comment['cnt'] . '次)" target="_blank"';
		if($i == 1){
			$s .= '<a class="item-top item-'.$i.'" '.$avatarurl.'><h4>【金牌读者】<small>评论：' . $comment['cnt'] . '</small></h4>'.$avatarload.'<strong>' . $comment['comm_Name'] . '</strong>'.$comment['comm_HomePage'] . '</a>';
	   }
	 	elseif($i == 2){
			$s .= '<a class="item-top item-'.$i.'" '.$avatarurl.'><h4>【银牌读者】<small>评论：' . $comment['cnt'] . '</small></h4>'.$avatarload.'<strong>' . $comment['comm_Name'] . '</strong>'.$comment['comm_HomePage'] . '</a>';
	   }  
	 	elseif($i == 3){
			$s .= '<a class="item-top item-'.$i.'" '.$avatarurl.'><h4>【铜牌读者】<small>评论：' . $comment['cnt'] . '</small></h4>'.$avatarload.'<strong>' . $comment['comm_Name'] . '</strong>'.$comment['comm_HomePage'] . '</a>';
	   }     
	   else{
			$s .= '<a '.$avatarurl.'>'.$avatarload.'' . $comment['comm_Name'] . '</a>';
		}
		
		}
		$s .= '<!--您所查看的是读者墙缓存，缓存时间'.date('Y-m-d h:i:sa',time()).'-->';
	file_put_contents($zbp->usersdir . 'cache/ydblog_readers.txt', $s);
}
//标签云页面
function ydblog_page_tagss_cache() {
	global $zbp;
	$s = '';
	$i = 100;
	$array = $zbp->GetTagList('','',array('tag_Count'=>'DESC'),array($i),'');
	foreach ($array as $tag) {
		$s .= '<li><a class="tagname" href="'.$tag->Url.'">"'.$tag->Name.'"</a><strong>x '.$tag->Count.'</strong><br><a href="'.$tag->Url.'">'.$tag->Alias.''.$tag->Intro.'</a><br><span class="muted"></span></li>';
	}
	$s .= '<!--您所查看的是标签云缓存，缓存时间'.date('Y-m-d h:i:sa',time()).'-->';
	file_put_contents($zbp->usersdir . 'cache/ydblog_tagss.txt', $s);
}
//文章归档页面缓存
function ydblog_page_archive_cache() {
	global $zbp;
	$i = $zbp->modulesbyfilename['archives']->MaxLi;
	if($i<0)return '';
	$fdate;
	$ldate;
	$sql = $zbp->db->sql->Select($zbp->table['Post'], array('log_PostTime'), null, array('log_PostTime' => 'DESC'), array(1), null);
	$array = $zbp->db->Query($sql);
	if (count($array) == 0)
		return '';
	$ldate = array(date('Y', $array[0]['log_PostTime']), date('m', $array[0]['log_PostTime']));
	$sql = $zbp->db->sql->Select($zbp->table['Post'], array('log_PostTime'), null, array('log_PostTime' => 'ASC'), array(1), null);
	$array = $zbp->db->Query($sql);
	if (count($array) == 0)
		return '';
	$fdate = array(date('Y', $array[0]['log_PostTime']), date('m', $array[0]['log_PostTime']));
	$arraydate = array();
	for ($i = $fdate[0]; $i < $ldate[0] + 1; $i++) {
		for ($j = 1; $j < 13; $j++) {
			$arraydate[] = strtotime($i . '-' . $j);
		}
	}
	foreach ($arraydate as $key => $value) {
		if ($value - strtotime($ldate[0] . '-' . $ldate[1]) > 0)
			unset($arraydate[$key]);
		if ($value - strtotime($fdate[0] . '-' . $fdate[1]) < 0)
			unset($arraydate[$key]);
	}
	$arraydate = array_reverse($arraydate);
	$s = '';
  $s .='<article class="archives">';
	foreach ($arraydate as $key => $value) {
		$url = new UrlRule($zbp->option['ZC_DATE_REGEX']);
		$url->Rules['{%date%}'] = date('Y-n', $value);
		$url->Rules['{%year%}'] = date('Y', $value);
		$url->Rules['{%month%}'] = date('n', $value);
		$url->Rules['{%day%}'] = 1;
		$fdate = $value;
		$ldate = (strtotime(date('Y-m-t', $value)) + 60 * 60 * 24);
		$sql = $zbp->db->sql->Count($zbp->table['Post'], array(array('COUNT', '*', 'num')), array(array('=', 'log_Type', '0'), array('=', 'log_Status', '0'), array('BETWEEN', 'log_PostTime', $fdate, $ldate)));
		$n = GetValueInArrayByCurrent($zbp->db->Query($sql), 'num');
		if ($n > 0) {
		$s.='<div class="item">';
		$s.='<h3>'.ydblog_GetChineseMonth(date('n', $fdate)).'月 '.date('Y', $fdate).'年</h3>';
		$s.='<ul class="archives-list">';
	$order = 	array('log_PostTime'=>'DESC');
	$where = array(
          array('=','log_Status','0'),
          array('=','log_Type','0'),
          array('BETWEEN', 'log_PostTime', $fdate, $ldate)
          );
			$nbarraylist = $zbp->GetArticleList(array('*'),$where,$order,'',''); 
      foreach ($nbarraylist as $key=>$article){
      $s .= ' <li><time>'.$article->Time('d').'日</time><a href="'.$article->Url.'">'.$article->Title.'</a><span class="muted">'.$article->CommNums.'评论</span></li>';
      }
$s.='</ul></div>';
		}
	}
	$s .= '</article><!--您所查看的是文章归档缓存，缓存时间'.date('Y-m-d h:i:sa',time()).'-->';

	file_put_contents($zbp->usersdir . 'cache/ydblog_archive.txt', $s);
}